package com.leetcode.www;

import java.util.Arrays;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-01-10
 * Time:15:00
 */
public class Normal27 {
    public static void main(String[] args) {
        int[] nums = {1,2,3,4,8,1,2,1,4};
        int val = 1;
        int l = 0;
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if(nums[i] != val){
                nums[l] = nums[i];
                nums[i] = 0;
                l++;
            }
        }
        //其实应该输出l下标以前的数字 l 以后的数字  已经无效了
        System.out.println(Arrays.toString(nums));
    }
    public int removeElement(int[] nums, int val) {
        if (nums == null || nums.length == 0) return 0;
        /*if (nums.length == 1 && nums[0] == val) {
            return 0;
        } else {
            return 1;
        }*/
        //把所有等于val的值与后面的值进行交换
        int x = 0;
        int h = nums.length - 1;
        while (x < h) {
            if (nums[x] != val) {
                x++;
            } else {
                while (nums[h] == val) {
                    h--;
                }
                nums[x] = nums[h];
                nums[h] = 0;
                x++;
                h--;
            }
            if (x == h) {
                break;
            }
        }
        if (nums[x] == val) {
            return x - 1;
        } else {
            return x;
        }
    }
}
